home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / win / winseria.zip / WINSERIA.C
C/C++ Source or Header  |  1993-09-07  |  4KB  |  135 lines

  1. #define STRICT
  2. //
  3. // winseria.c - Getting the drive Serial Number under Window.
  4. //
  5. /*
  6.  
  7.   This file shows an example of calling a real mode interrupt
  8.   ( 0x21, 0x69 ) from within a Windows application to determine
  9.   the Serial Number, Volume Label, and File System Type ( which
  10.   is either "FAT12" or "FAT16". The Info Level will be 0. 
  11.  
  12.   One of the main issues involved is to create two pointers to
  13.   the same memory location - a selector address which is
  14.   useable by Windows, and a segment address which is useable by
  15.   DOS. This is done using the GlobalDOSAlloc() function, which
  16.   returns both the selector and the segment address of the
  17.   allocated memory. Note that this function should not be used
  18.   for most allocation because it allocates a non-moveable
  19.   memory block from the base 1Meg of memory.
  20.  
  21.   The segment address is then passed to DOS int 21h, 69h to get
  22.   information regarding the drive. The selector address is then
  23.   used by the Windows parts of the code to access that data.
  24.  
  25. */ 
  26.  
  27. #include <dos.h>      // MK_FP
  28. #include <stdlib.h>   // ltoa()
  29. #include <string.h>   // strncpy(), strcpy(), strup()
  30. #include <windows.h>  // wsprintf()
  31.  
  32. // Structure to containt the register values for the
  33. //    DPMI services
  34. typedef struct
  35. {
  36.   DWORD di,si,bp,res1,bx,dx,cx,ax;
  37.   WORD flags, es,ds,fs,gs,res2,res3,sp,ss;
  38. } DPMIRegs;
  39.  
  40. // Stucture to contain the disk information
  41. typedef struct {
  42.   unsigned int InfoLevel;
  43.   unsigned long Serial;
  44.   unsigned char VolumeLabel[11];
  45.   unsigned char FileSystemType[8];
  46. } Info;
  47.  
  48. // Create globally to initialize to Zero
  49. DPMIRegs r;
  50.  
  51. // Use DPMI services to access Real mode
  52. void SimRealInt (BYTE i, DPMIRegs far *p)
  53. {
  54.   asm {
  55.     mov bl, [i]    // DOS interupt
  56.     mov bh, 0
  57.     les di, [p]    // Register Values
  58.     mov cx, 0
  59.     mov ax, 0300h
  60.     int 31h
  61.   }
  62. }
  63.  
  64. #pragma argsused
  65. int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE
  66.                     hPrevInstance, LPSTR lpCmdLine, int
  67.                     nCmdShow )
  68. {
  69.    int       Drive=3 ;      // 3 = C drive
  70.    DWORD     dwValue ;
  71.    unsigned  size ;
  72.    char      temp[5] ;
  73.    char      diskInfo[120] ;
  74.    char      Label[12] ;
  75.    char      Type[9] ;
  76.    char      Serial[10] ;
  77.  
  78.    WORD      wSegment, wSelector ;
  79.    Info far  *WindowsPtr ;
  80.  
  81.    size = sizeof( Info ) ;
  82.  
  83.    dwValue = GlobalDosAlloc( size ) ;
  84.    if (!dwValue)
  85.    {
  86.      MessageBox( NULL, "Could not Allocate Memory", "Error",
  87.                  MB_ICONINFORMATION | MB_OK ) ;
  88.      return 1 ;
  89.    }
  90.  
  91.    wSegment   = HIWORD(dwValue) ;
  92.    wSelector  = LOWORD(dwValue) ;
  93.    WindowsPtr = (Info far *)MK_FP(wSelector,0) ;
  94.  
  95.    r.ax = 0x6900;
  96.    r.bx = Drive ;
  97.    r.dx = 0;
  98.    r.ds  = wSegment;
  99.    SimRealInt (0x21, &r) ; // Call DOS int 21h, 69h
  100.  
  101.    // Check for read error
  102.    if ( r.flags & 0x01)
  103.    {
  104.       WindowsPtr->Serial = 0;
  105.    }
  106.  
  107.    // Add a NULL terminator to the strings which were returned
  108.    //    in the Info structure.
  109.    _fstrncpy( Label, (char far *)WindowsPtr->VolumeLabel, 11 ) ;
  110.    Label[11] = 0 ;
  111.    _fstrncpy( Type, (char far *)WindowsPtr->FileSystemType, 8 ) ;
  112.    Type[8] = 0 ;
  113.    ltoa( WindowsPtr->Serial, Serial, 16 ) ;
  114.    _fstrcpy( temp, &Serial[4] ) ;
  115.    _fstrcpy( &Serial[5], temp ) ;
  116.    _fstrupr( Serial ) ; // Convert all characters to UPPER CASE
  117.    Serial[4] = ':' ;
  118.    Serial[9] = 0 ;
  119.    // Format the string to be displayed.
  120.    wsprintf( diskInfo, "Drive:\t\t%c\r\n"
  121.                   "Serial Number:\t%s\r\n"
  122.                   "Volume Label:\t%s\r\n"
  123.                   "File System type:\t%s",
  124.                   (Drive + 64), (char far*)Serial,
  125.                   (char far *)Label, (char far *)Type ) ;
  126.  
  127.    MessageBox( NULL, (char far *)diskInfo, "Disk Info",
  128.                MB_ICONINFORMATION | MB_OK ) ;
  129.    
  130.    GlobalDosFree(wSelector) ;
  131.  
  132.    return 0 ;
  133. }
  134.  
  135.